package bll

import (
	"auth/event"
	"auth/model"
	"auth/model/entity"
	"auth/store"
	"auth/store/postgres"
)

type authorityMenu struct {
	iAuthorityMenu store.IAuthorityMenu
}

var AuthorityMenu = &authorityMenu{}

func (a *authorityMenu) init() func() {
	a.iAuthorityMenu = postgres.AuthorityMenu
	return func() {}
}

func (a *authorityMenu) onEvent(*event.Data) {}

func (a *authorityMenu) Create(in *model.AuthorityMenuCreateRequest) error {
	var (
		err error
	)

	c := buildAuthorityMenu(in)
	err = a.iAuthorityMenu.Create(c)
	return err
}

func (a *authorityMenu) List(in *model.AuthorityMenuListRequest) (*model.AuthorityMenuListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.AuthorityMenu
		out   = &model.AuthorityMenuListResponse{}
	)

	if total, list, err = a.iAuthorityMenu.List(in); err != nil {
		return nil, err
	}

	out.Total = total
	out.Data = model.AuthorityMenusEntityToDto(list)

	return out, err
}

func buildAuthorityMenu(in *model.AuthorityMenuCreateRequest) *entity.AuthorityMenu {
	return &entity.AuthorityMenu{

		MenuId: in.MenuId,

		AuthorityId: in.AuthorityId,
	}
}
